﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;

namespace Hangout.Client.Notifications
{
    public partial class Notifications : PhoneApplicationPage
    {


       

        public Notifications()
        {
            InitializeComponent();
        }

        #region NotificationControl

        public void AttachEvents()
        {
            try
            {
                
                NotificationControl.NavigateToHangout += new Controls.Notifications.NotificationReceived.IdHelper(NotificationControl_NavigateToHangout);
                NotificationControl.NavigateToProfile += new Controls.Notifications.NotificationReceived.IdHelper(NotificationControl_NavigateToProfile);
                NotificationList.NavigateToHangout += new Controls.Notifications.NotificationList.IdHelper(NotificationControl_NavigateToHangout);
                NotificationList.NavigateToProfile += new Controls.Notifications.NotificationList.IdHelper(NotificationControl_NavigateToProfile);
            }
            catch (System.Exception ex)
            {
                Core.Exceptions.ExceptionReporting.Report(ex);
                MessageBox.Show(ErrorText.GenericErrorText, ErrorText.GenericErrorCaption, MessageBoxButton.OK);

            }
        }

        

       

        void NotificationControl_NavigateToProfile(int id)
        {
            try
            {
                NavigateToViewProfile(id);
            }
            catch (System.Exception ex)
            {
                Core.Exceptions.ExceptionReporting.Report(ex);
                MessageBox.Show(ErrorText.GenericErrorText, ErrorText.GenericErrorCaption, MessageBoxButton.OK);

            }
        }

        void NotificationControl_NavigateToHangout(int id)
        {
            try
            {
                NavigateToHangout(id);
            }
            catch (System.Exception ex)
            {
                Core.Exceptions.ExceptionReporting.Report(ex);
                MessageBox.Show(ErrorText.GenericErrorText, ErrorText.GenericErrorCaption, MessageBoxButton.OK);

            }
        }

        private void NavigateToHangout(int id)
        {
            try
            {
                //navigate to hangout page
                Dispatcher.BeginInvoke(() =>
                {
                    NavigationService.Navigate(new Uri(Navigation.Hangout + "?id=" + id, UriKind.RelativeOrAbsolute));
                });
            }
            catch (System.Exception ex)
            {
                Core.Exceptions.ExceptionReporting.Report(ex);
                MessageBox.Show(ErrorText.GenericErrorText, ErrorText.GenericErrorCaption, MessageBoxButton.OK);

            }
        }

        private void NavigateToViewProfile(int id)
        {
            try
            {
                //navigate to hangout page
                Dispatcher.BeginInvoke(() =>
                {
                    NavigationService.Navigate(new Uri(Navigation.ViewProfile + "?id=" + id, UriKind.RelativeOrAbsolute));
                });
            }
            catch (System.Exception ex)
            {
                Core.Exceptions.ExceptionReporting.Report(ex);
                MessageBox.Show(ErrorText.GenericErrorText, ErrorText.GenericErrorCaption, MessageBoxButton.OK);

            }
        }

        #endregion

        private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
        {
            try
            {
                AttachEvents();
                LoadItems();
                NotificationList.ItemList.Height = 650;
            }
            catch (Exception ex)
            {
                Core.Exceptions.ExceptionReporting.Report(ex);
                MessageBox.Show(ErrorText.GenericErrorText, ErrorText.GenericErrorCaption, MessageBoxButton.OK);
            }
           
            
        }

        void NotificationList_ScrolledDown()
        {
            try
            {
                LoadItems();
            }
            catch (Exception ex)
            {
                Core.Exceptions.ExceptionReporting.Report(ex);
                MessageBox.Show(ErrorText.GenericErrorText, ErrorText.GenericErrorCaption, MessageBoxButton.OK);
            }
        }

        private void LoadItems()
        {
            try
            {
                NotificationList.LoadingPG.Visibility = System.Windows.Visibility.Visible;
                Services.NotificationServiceClient.GetNotificationsCompleted += new EventHandler<NotificationService.GetNotificationsCompletedEventArgs>(client_GetNotificationsCompleted);
                Services.NotificationServiceClient.GetNotificationsAsync(Settings.Settings.UserID, NotificationList.take, NotificationList.skip,Settings.Settings.FacebookAccessToken);
                NotificationList.skip += NotificationList.take;

            }
            catch (Exception ex)
            {
                Core.Exceptions.ExceptionReporting.Report(ex);
                MessageBox.Show(ErrorText.GenericErrorText, ErrorText.GenericErrorCaption, MessageBoxButton.OK);
            }
        }

        void client_GetNotificationsCompleted(object sender, NotificationService.GetNotificationsCompletedEventArgs e)
        {
            Services.NotificationServiceClient.GetNotificationsCompleted -= new EventHandler<NotificationService.GetNotificationsCompletedEventArgs>(client_GetNotificationsCompleted);
            try
            {
                Services.NotificationServiceClient.MarkAllAsReadAsync(Settings.Settings.UserID,Settings.Settings.FacebookAccessToken);
                NotificationList.LoadingPG.Visibility = System.Windows.Visibility.Collapsed;
                if (e.Error == null)
                {
                    if (e.Result.Count > 0)
                    {
                        NotificationList.Notifications.AddRange(e.Result.ToList());

                    }
                    NotificationList.RefreshList();
                }
                else
                {
                    MessageBox.Show(ErrorText.Description, ErrorText.Caption, MessageBoxButton.OK);
                }
            }
            catch (Exception ex)
            {
                Core.Exceptions.ExceptionReporting.Report(ex);
                MessageBox.Show(ErrorText.GenericErrorText, ErrorText.GenericErrorCaption, MessageBoxButton.OK);
            }
        }

        private void ApplicationBarIconButton_Click(object sender, EventArgs e)
        {
            try
            {
                //load more items 
                LoadItems();
            }
            catch (Exception ex)
            {
                Core.Exceptions.ExceptionReporting.Report(ex);
                MessageBox.Show(ErrorText.GenericErrorText, ErrorText.GenericErrorCaption, MessageBoxButton.OK);
            }
        }

        private void ApplicationBarIconButton_Click_1(object sender, EventArgs e)
        {
            try
            {
                //navigate to hangout page
                Dispatcher.BeginInvoke(() =>
                {
                    NavigationService.Navigate(new Uri(Navigation.Dashboard, UriKind.RelativeOrAbsolute));
                });
            }
            catch (System.Exception ex)
            {
                Core.Exceptions.ExceptionReporting.Report(ex);
                MessageBox.Show(ErrorText.GenericErrorText, ErrorText.GenericErrorCaption, MessageBoxButton.OK);

            }
        }
    }
}